package com.example.myapplication.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.example.myapplication.BaseOnclickListener;
import com.example.myapplication.R;
import com.example.myapplication.entity.PlayList;

import java.util.List;

public class PlayListAdapter extends RecyclerView.Adapter<PlayListAdapter.PlayListViewHolder> {
    private PlayList playLists;
    private Context context;
    private List<PlayList.TracksBean> tracks;
    private BaseOnclickListener onclickListener;

    public PlayListAdapter() {
    }

    public void setData(List<PlayList.TracksBean> tracks){
        this.tracks = tracks;
        notifyDataSetChanged();
    }

    @NonNull
    @Override
    public PlayListViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return new PlayListViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.item_play_list, parent, false));
    }

    @Override
    public void onBindViewHolder(@NonNull PlayListViewHolder holder, int position) {
        PlayList.TracksBean tracksBean = tracks.get(position);

        holder.tvIndex.setText(String.valueOf(position + 1));
        holder.tvSong.setText(tracksBean.getName());
        holder.tvSinger.setText(tracksBean.getAr().get(0).getName() + " - ");
        holder.tvAlbum.setText(tracksBean.getAl().getName());

        holder.view.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (onclickListener != null){
                    onclickListener.click(position);
                }
            }
        });
    }

    @Override
    public int getItemCount() {
        return tracks == null ? 0 : tracks.size();
    }

    class PlayListViewHolder extends RecyclerView.ViewHolder{
        TextView tvIndex;
        TextView tvSong;
        TextView tvSinger;
        TextView tvAlbum;
        View view;

        public PlayListViewHolder(@NonNull View itemView) {
            super(itemView);

            tvIndex = itemView.findViewById(R.id.tv_index);
            tvSong = itemView.findViewById(R.id.tv_song_name);
            tvSinger = itemView.findViewById(R.id.tv_singer_name);
            tvAlbum = itemView.findViewById(R.id.tv_album_name);
            view = itemView.findViewById(R.id.view_layout);
        }
    }

    public void setBaseOnclickListener(BaseOnclickListener onclickListener){
        this.onclickListener = onclickListener;
    }
}
